package com.best.member.utils;

import com.best.core.utils.RedisUtil;
import com.best.member.input.dto.UserRegisterDTO;
import com.best.member.serice.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @ClassName: Test
 * @description: TODO
 * @author: 李东
 * @Date: 2020-7-1 18:00
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class Test {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private MemberService memberService;

    @org.junit.Test
    public void testR() throws InterruptedException {

        for (int i=0;i<20;i++){
            this.testA(i);
                        Thread.sleep(1000);
        }

    }

    @Async
    public void testA(int i) throws InterruptedException {
        String key = "lock";
        if (redisUtil.lock(key,10)) {
            log.info("success:获取锁成功:{}",i+1);

//            Thread.sleep(10);

            redisUtil.unLock(key);

            log.info("锁释放成功:{}",i+1);

        } else {
            log.info("defeat:未获取到锁:{}",i+1);
        }
    }


    @org.junit.Test
    public void insertUser(){
        UserRegisterDTO dto = new UserRegisterDTO();
        dto.setMobile("123123");
        dto.setPassword("123123");

        memberService.register(dto);
    }



}
